﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace elevator
{
    class MySQLConnector
    {
        public MySQLConnector()
        {
            connectToDB(); 
        }

        private void connectToDB()
        {
            string external = "Server=db4free.net;Database=cp2013;UID=cp2013;Password=cp2013";
            MySqlConnection connection = new MySqlConnection(external);
            try
            {
                connection.Open();
                connection.Close();
                //MessageBox.Show("Done");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 
        }

        public List<String> getSimNames()
        {
            //SELECT DICTINCT name FROM simulation; 
            string strConnection = "Server=db4free.net;Database=cp2013;UID=cp2013;Password=cp2013";
            MySqlConnection connection = new MySqlConnection(strConnection);

            List<string> array = new List<string>();

            using (MySqlCommand cmd = new MySqlCommand("SELECT DISTINCT name from simulation", connection))
            {
                try
                {
                    connection.Open(); 
                    using (MySqlDataReader Reader = cmd.ExecuteReader())
                    {
                        while (Reader.Read())
                        {
                            array.Add(Reader["name"].ToString());
                        }
                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show("error: " + ex);
                }
            }
            connection.Close(); 
            return array; 
        }

        public void saveData(string[] data)
        {
            string strConnection = "Server=db4free.net;Database=cp2013;UID=cp2013;Password=cp2013";
            MySqlConnection connection = new MySqlConnection(strConnection);

            using (MySqlCommand cmd = new MySqlCommand("INSERT INTO simulation values('" + data[0] + "','" + data[1] + "','" + data[2] + "','" + data[3] + "','" + data[4] + "')", connection))
            {
                connection.Open();
                cmd.ExecuteNonQuery(); 
            }
            connection.Close();
        }

        public List<string[]> getSimSteps(string simName)
        {
            string strConnection = "Server=db4free.net;Database=cp2013;UID=cp2013;Password=cp2013";
            MySqlConnection connection = new MySqlConnection(strConnection);
            List<string[]> array = new List<string[]>();
            
            using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM simulation WHERE name = '" + simName + "'", connection))
            {
                try
                {
                    connection.Open();
                    using (MySqlDataReader Reader = cmd.ExecuteReader())
                    {
                        while (Reader.Read())
                        {
                            String[] row = { Reader["name"].ToString(), Reader["cycle"].ToString(), Reader["person"].ToString(), Reader["origin"].ToString(), Reader["destination"].ToString() };
                            array.Add(row);
                        }
                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show("error: " + ex);
                }
            }
            connection.Close();
            return array; 
        }

    }
}
